<?php

namespace App\Listeners\Admin\V1;

use App\Events\Admin\V1\LoginEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LoginListeners
{
    /**
     * @var
     */
    protected $login_user;

    /**
     * @var
     */
    protected $login_token;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * @param LoginEvent $event
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/24/14:53
     */
    public function handle(LoginEvent $event)
    {
        $this->login_user = $event->getUser();
        $this->login_token = $event->getToken();
        $this->toLogin();
    }

    /**
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/24/14:53
     */
    public function toLogin()
    {
        $now = now();
        $clientUA = get_app_ua();
        $clientIp = request()->getClientIp();
        $this->login_user->login_count ++;
        $this->login_user->current_login_time = $now;
        $this->login_user->current_login_ip = $clientIp;
        $this->login_user->current_login_ua = $clientUA;
        $this->login_user->last_token = $this->login_token;
        $this->login_user->last_login_time = $this->login_user->current_login_time ?? $now;
        $this->login_user->expired_time = time() + config('common.listener.admin.expired_time');
        $this->login_user->last_login_time = $this->login_user->current_login_time ?? $now;
        $this->login_user->last_login_ip = $this->login_user->current_login_ip ?? $clientIp;
        $this->login_user->last_login_ua = $this->login_user->current_login_ua ?? $clientUA;
        $this->login_user->save();
    }
}
